/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlayerChromosomeComponent.java
 *
 * Created on 9/Dez/2011, 14:19:19
 */

package intopo.evogame.partnerselection.tools.config;

import intopo.evogame.partnerselection.config.TextConfigFilePlainPlayersEditor;
import java.awt.Color;
import java.awt.Component;
import java.util.EventObject;
import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.EventListenerList;

/**
 * The component that displays the chromosome parameters of a player in the configuration file.  This component is used as the renderer and the editor for the table that displays the players in a node.
 * The user can edit the quantity, the chromosome and the strategy.  The chromosome is optionaly as default genes can be used for the chromosome.
 *
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 9/Dez/2011
 */
public class PlayerChromosomeEditorRenderer
	extends javax.swing.AbstractCellEditor
	implements
		javax.swing.table.TableCellRenderer,
		javax.swing.table.TableCellEditor
{
	final private PlayerChromosomeComponent component;

//	NodeParameters.Player.Chromosome chromosome;
//
	private EventListenerList eventListener;
	/**
	 * Creates new form PlayerChromosomeComponent
	 */
	public PlayerChromosomeEditorRenderer ()
	{
		this.component = new PlayerChromosomeComponent ();
//		chromosome = new NodeParameters.Player.Chromosome ();
		this.eventListener = new EventListenerList ();
	}

//	protected void fireCellChanged ()
//	{
//
//		// Guaranteed to return a non-null array
//		Object[] listeners = this.listenerList.getListenerList ();
//		// Process the listeners last to first, notifying
//		// those that are interested in this event
//		for (int i = listeners.length-2; i>=0; i-=2) {
//			if (listeners[i]==FooListener.class) {
//			// Lazily create the event:
//			if (fooEvent == null)
//			fooEvent = new FooEvent(this);
//			((CellEditorListener)listeners[i+1]).fooXXX(fooEvent);
//			}
//		}
//	}
	@Override
	public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
	{
		this.component.setChromosome ((TextConfigFilePlainPlayersEditor.Chromosome) value);
		if (isSelected) {
			this.component.setLabelsVisible (hasFocus);
			this.component.setBorder (BorderFactory.createLineBorder (Color.BLACK, (hasFocus ? 3 : 1)));
		}
		else {
			this.component.setLabelsVisible (false);
			this.component.setBorder (BorderFactory.createEmptyBorder ());
		}
		return this.component;
	}
	@Override
	public Component getTableCellEditorComponent (JTable table, Object value, boolean isSelected, int row, int column)
	{
		this.component.setChromosome ((TextConfigFilePlainPlayersEditor.Chromosome) value);
		this.component.setLabelsVisible (isSelected);
		if (isSelected) {
			this.component.setBorder (BorderFactory.createEtchedBorder ());
		}
		else {
			this.component.setBorder (BorderFactory.createEmptyBorder ());
		}
		return this.component;
	}

	@Override
	public Object getCellEditorValue ()
	{
		TextConfigFilePlainPlayersEditor.Chromosome c = this.component.getChromosome ();
		System.out.println ("getCellEditorValue() " + c);
		return this.component.getChromosome ();
	}

	@Override
	public boolean isCellEditable (EventObject anEvent)
	{
		return true;
	}

	@Override
	public boolean shouldSelectCell (EventObject anEvent)
	{
		return true;
	}

	@Override
	public boolean stopCellEditing ()
	{
		if (this.component.getChromosome () != null) {
			this.fireEditingStopped ();
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public void cancelCellEditing ()
	{
	}

	@Override
	public void addCellEditorListener (CellEditorListener l)
	{
		this.eventListener.add (CellEditorListener.class, l);
	}

	@Override
	public void removeCellEditorListener (CellEditorListener l)
	{
		this.eventListener.remove (CellEditorListener.class, l);
	}
}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
